package com.cheyun.netsalev3.view

import android.os.Bundle
import android.widget.LinearLayout
import com.cheyun.netsalev3.R
import com.cheyun.netsalev3.databinding.ActivityAnalysisBinding
import com.cheyun.netsalev3.utils.BaseActivity
import com.just.agentweb.AgentWeb
import android.text.TextUtils
import android.webkit.WebView
import com.cheyun.netsalev3.tencenttim.utils.DemoLog
import com.cheyun.netsalev3.utils.MySharedPreferences
import com.cheyun.netsalev3.utils.api.ApiStores
import com.just.agentweb.WebChromeClient
import java.net.URLEncoder
import android.graphics.Bitmap
import com.just.agentweb.WebViewClient


class AnalysisActivity : BaseActivity<ActivityAnalysisBinding>() {
    override fun initContentView(): Int {
        return R.layout.activity_analysis
    }

    override fun initView() {
        binding.leftBut.setOnClickListener {
            finish()
        }
    }
    var mAgentWeb:AgentWeb? = null
    override fun initData() {
        var cauthkey = MySharedPreferences.getCauthkey()
        cauthkey = URLEncoder.encode(cauthkey)
        mAgentWeb = AgentWeb.with(this)//传入Activity
                .setAgentWebParent(binding.llWebview,  LinearLayout.LayoutParams(-1, -1))//传入AgentWeb 的父控件 ，如果父控件为 RelativeLayout ， 那么第二参数需要传入 RelativeLayout.LayoutParams
                .useDefaultIndicator()
                .setSecurityType(AgentWeb.SecurityType.STRICT_CHECK)
                .createAgentWeb()
                .ready()
                .go( ApiStores.ANALYSIS_URL+"?cauthkey="+cauthkey)

//        ApiStores.ANALYSIS_URL+"?cauthkey="+cauthkey

        DemoLog.i("AnalysisActivity",ApiStores.ANALYSIS_URL+"?cauthkey="+cauthkey)

        mAgentWeb?.webCreator?.webView?.setWebChromeClient(object : WebChromeClient() {
             override fun onReceivedTitle(view: WebView, title: String) {
                if (!TextUtils.isEmpty(title)) {
                    binding.titleText.text = title
                }
                super.onReceivedTitle(view, title)
            }

            override fun onProgressChanged(view: WebView?, newProgress: Int) {
                super.onProgressChanged(view, newProgress)
                DemoLog.i("AnalysisActivity","newProgress:"+newProgress)
                mAgentWeb?.indicatorController?.setProgress(newProgress)
            }
        })
    }

    private val mWebViewClient = object : WebViewClient() {
        override  fun onPageStarted(view: WebView, url: String, favicon: Bitmap) {
            DemoLog.i("AnalysisActivity","url:"+url)
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
//        if (savedInstanceState == null) {
//            supportFragmentManager.beginTransaction()
//                    .replace(R.id.container, HomeAnalysisFragment.newInstance())
//                    .commitNow()
//        }
    }

    override fun onResume() {
        mAgentWeb?.webLifeCycle?.onResume()
        super.onResume()
    }

    override fun onPause() {
        mAgentWeb?.webLifeCycle?.onPause()
        super.onPause()
    }

    override fun onDestroy() {
        mAgentWeb?.webLifeCycle?.onDestroy()
        super.onDestroy()
    }

}
